Group Test Cases in TestNG
package asc; //import org.testng.annotations.AfterMethod; //import org.testng.annotations.AfterTest; //import org.testng.annotations.BeforeMethod; //import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; @Test(groups="user-regristration") public class groupTest { @Test(priority = 1,groups="regression") public void Test1() { System.out.println("Test 1"); } @Test(priority = 2,groups="regression") public void Test2() { System.out.println("Test 2"); } @Test(groups={"regression","bvt"}) public void Test3() { System.out.println("Test 3"); } @Test(groups="bvt") public void Test4() { System.out.println("Test 4"); } }
Class and Package Declaration
package asc; import org.testng.annotations.Test;
- The class groupTest is part of the asc package.
- The
org.testng.annotations.Test
annotation is imported, which is essential for creating test methods in TestNG.
Class-Level @Test Annotation with Groups
@Test(groups="user-regristration") public class groupTest {
- The class groupTest is annotated with
@Test(groups="user-regristration")
- This means that all test methods within this class are automatically associated with the user- regristration group, in addition to any groups specified at the method level.
Test Methods with Priority and Groups
- Test1 Method
@Test(priority = 1,groups="regression") public void Test1() { System.out.println("Test 1"); }
- priority = 1: This specifies that Test1 should be executed first among the tests in this class.
- groups = " regression ": This method is part of the regression group.
- Test2 Method
@Test(priority = 2,groups="regression") public void Test2() { System.out.println("Test 2"); }
- priority = 2: This specifies that Test2 should be executed after Test1.
- groups = " regression ": This method is also part of the regression group.
- Test3 Method
@Test(groups={"regression","bvt"}) public void Test3() { System.out.println("Test 3"); }
groups={"regression","bvt"}:
This method belongs to both the regression and bvt (Build Verification Testing) groups.
- Test4 Method
@Test(groups="bvt") public void Test4() { System.out.println("Test 4"); }
- groups = " bvt ": This method is part of the bvt group.
Summary
- Grouping: TestNG allows you to group test methods using the groups attribute. You can assign one or more groups to a test method or an entire class.
- Execution Order: The priority attribute determines the order in which the tests are executed within a group or class.
- Group Inheritance: The user - regristration group applied at the class level means all test methods are part of this group, in addition to any other groups they are explicitly assigned to.
This grouping mechanism helps organize and selectively run subsets of tests, making it easier to manage large test suites.